//
//  RVBonusListController.swift
//  AirTravel
//
//  Created by 许必成 on 2021/11/27.
//

import UIKit
import HandyJSON
enum RVBonusOrderType {
    case all//全部
    case diliver//代发货
    case accept//待收货
    case finish//已完成
}

class RVBonusListController: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!
    
    var type: RVBonusOrderType
    
    var dataSource: [RVBonusOrderList] = []
    
    init(type: RVBonusOrderType){
        
        self.type = type
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        confUI()
    }
    
    func confUI(){
        tableView.register(UINib(nibName: "RVBonusOrderListCell", bundle: nil), forCellReuseIdentifier: "interorder_list")
        tableView.rowHeight = 225
        tableView.reloadData()
        getDatas()
    }
    
    func getDatas(){
        var parameters: [String: Any] = [String: Any]()
        var orderType: String = ""
        switch type {
        case .all:
            orderType = "0"
        case .diliver:
            orderType = "10"
        case .accept:
            orderType = "20"
        case .finish:
            orderType = "30"
        }
        parameters["order_status"] = orderType
        YProgressHUD.show()
        NetWorkRequest(API1.getBonusOrder(parameters: parameters)) { json in
            
            YProgressHUD.dismiss()
            guard let data = JSONDeserializer<RVBonusOrderData>.deserializeFrom(dict: json.dictionaryObject) else{
                return
            }
            self.dataSource = data.data
            self.tableView.reloadData()
            
        } failed: { msg in
            YProgressHUD.dismiss()

        } errorResult: {
            YProgressHUD.dismiss()
        }

    }

}

extension RVBonusListController: UITableViewDelegate, UITableViewDataSource{
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "interorder_list", for: indexPath) as! RVBonusOrderListCell
        
        let list = dataSource[indexPath.row]
        cell.orderNoLabel.text = "订单编号: " + list.order_sn
        cell.statusLabel.text = list.order_status_text
        cell.descLabel.text = list.goods_text_array.good_name
        cell.costLabel.text = list.goods_text_array.integral_price + "积分"
        let url = URL(string: list.goods_text_array.images)
        cell.imgView.kf.setImage(with: url)
        return cell
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}

